package util

import (
	"fmt"

	"gopkg.in/ini.v1"
)

// 解析配置文件并设置参数

var (
	OraServer   string
	OraPort     int
	OraCid      string
	Orausername string
	OraPassword string

	Server   string
	Port     string
	Userid   string
	Password string
	Database string

	DEPOSIT   int
	PREPAY    int
	ARREARAGE int
	SPDEPOSIT int

	CARDTYPE string

	RDPASSWD string
	RDTYPE   string
	RDLIB    string
)

func init() {
	// 解析配置文件config.ini
	file, err := ini.Load("config/config.ini")
	if err != nil {
		fmt.Printf("配置文件读取错误，请检查文件路径: %v\n", err)
	}

	// 读取数据库相关配置
	LoadDate(file)
	LoadRdAccount(file)
	LoadReaderCardInfo(file)
	LoadReader(file)
	LoadCardInfo(file)
}

func LoadDate(file *ini.File) {
	OraServer = file.Section("interlib").Key("server").MustString("")
	Orausername = file.Section("interlib").Key("username").MustString("")
	OraPassword = file.Section("interlib").Key("password").MustString("")
	OraPort = file.Section("interlib").Key("port").MustInt(1521)
	OraCid = file.Section("interlib").Key("servername").MustString("")
}

func LoadCardInfo(file *ini.File) {
	Server = file.Section("cardinfo").Key("server").MustString("")
	Port = file.Section("cardinfo").Key("port").MustString("")
	Userid = file.Section("cardinfo").Key("userid").MustString("")
	Password = file.Section("cardinfo").Key("password").MustString("")
	Database = file.Section("cardinfo").Key("database").MustString("")
}

func LoadRdAccount(file *ini.File) {
	DEPOSIT = file.Section("RDACCOUNT").Key("DEPOSIT").MustInt(0)
	PREPAY = file.Section("RDACCOUNT").Key("PREPAY").MustInt(0)
	ARREARAGE = file.Section("RDACCOUNT").Key("ARREARAGE").MustInt(0)
	SPDEPOSIT = file.Section("RDACCOUNT").Key("SPDEPOSIT").MustInt(0)
}

func LoadReaderCardInfo(file *ini.File) {
	CARDTYPE = file.Section("READER_CARDINFO").Key("CARDTYPE").MustString("0")
}

func LoadReader(file *ini.File) {
	RDPASSWD = file.Section("READER").Key("RDPASSWD").MustString("160018")
	RDTYPE = file.Section("READER").Key("RDTYPE").MustString("YKT_001:学生|YKT_002:教师")
	RDLIB = file.Section("READER").Key("RDLIB").MustString("SYZX")
}
